package com.itheima.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.SpecMapper;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {

    @Override
    public Result findAll() {

        List<Spec> specList = this.list();

        return new Result(true, StatusCode.OK,null,specList);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Spec> add(Spec spec) {
        if (this.getOne(new QueryWrapper<Spec>().eq("name", spec.getName())) == null&& !spec.getName().equals("")) {
            this.save(spec);
            return new Result(true, StatusCode.OK, "添加成功", null);
        } else {
            return new Result(false, StatusCode.ERROR, "规格名称不能重复或为空", null);
        }
    }

/*    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        Page<Spec> specPage = new Page<>(currentPage,pageSize);
        IPage<Spec> specIPage = this.page(specPage, new QueryWrapper<Spec>().like("name",queryString));
        return new PageResult(specIPage.getTotal(),specIPage.getRecords());
    }*/

    @Override
    public Result findById(Integer id) {
        try {
            Spec spec = this.getOne(new QueryWrapper<Spec>().eq("id", id));
            if (spec == null) {
                return new Result(false, StatusCode.ERROR, "没有此id,请重新输入", null);
            } else {
                return new Result(true, StatusCode.OK, "查询成功",spec);
            }
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
    }

    @Override
    public Result updateSpec(Integer id , Spec spec) {

        try {
            this.update(spec, new UpdateWrapper<Spec>().eq("id", id));

            return new Result(true, StatusCode.OK, "修改成功", null);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "修改失败", null);
        }
    }

    @Override
    public Result deleteSpec(Integer id) {
        try {
            this.removeById(id);
            return new Result(true, StatusCode.OK, "删除成功", null);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "删除失败", null);
        }
    }

    @Override
    public Result search(Integer page, Integer size) {

        try {
            IPage<Spec> specIPage = new Page<>(page,size);
            this.page(specIPage, new QueryWrapper<Spec>());
            return new Result(true, StatusCode.OK, null, new PageResult<Spec>(specIPage.getTotal(), specIPage.getRecords()));
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
    }

    @Override
    public Result searchMap(Integer page, Integer size, Object searchMap) {
        String jsonString = JSON.toJSONString(searchMap);
        Spec spec = JSON.parseObject(jsonString, Spec.class);
        IPage<Spec> specIPage = new Page<>(page, size);
        try {
            this.page(specIPage, new QueryWrapper<Spec>()
                    .like(!StringUtils.isEmpty(spec.getName()), "name", spec.getName())
                    .like(!StringUtils.isEmpty(spec.getOptions()), "options", spec.getOptions())
                    .like(!StringUtils.isEmpty(spec.getSeq()), "seq", spec.getSeq()));
            return new Result(true, StatusCode.OK, "查询成功", new PageResult<Spec>(specIPage.getTotal(), specIPage.getRecords()));
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
    }

}
